<?php
/**
 *  ObjFactory class unit tests
 *
 *  @author Jean-Lou Dupont
 *  @version @@package-version@@
 */

require_once 'PHPUnit/Framework.php';
require_once 'web-bloks/ObjFactory/ObjFactory.php';
require_once 'UnitTestClass.php';

class UnitTest extends PHPUnit_Framework_TestCase
{
	/**
	 * Destroys the singleton before each test.
	 */
	public function setup()
	{
		$obj1 = WB_ObjFactory::get();
		$obj1->__destruct();
	}
	
	public function testSingleton1()
	{
		$obj1 = TestClass::get();		
		$obj2 = TestClass::get();				
		
        $this->assertEquals( $obj1, $obj2 );		
	}
	
	public function testSingleton2()
	{
		$obj1 = TestClass::get();
		$obj2 = WB_ObjFactory::get();
			
        $this->assertEquals( $obj1, $obj2 );		
	}	

    public function testDerivedClass()
    {
    	$obj = TestClass::get();
		
		// Assert that the size of the Array fixture is 1.
        $this->assertEquals( 'TestClass', get_class( $obj ));
    }

	public function testDestructor()
	{
		$obj1 = TestClass::get();
		$obj1->__destruct();
				
        $this->assertEquals( 'TestClass', get_class( $obj1 ));				
				
		$obj2 = WB_ObjFactory::get();				
        $this->assertEquals( 'WB_ObjFactory', get_class( $obj2 ));				
	}
	
	public function testFactory1()
	{
		$obj1 = TestClass::get();
		
		$result = $obj1->factory( 'whatever' );		
        $this->assertEquals( 'bidon', $result );			
	}

	public function testFactory2()
	{
		$obj1 = TestClass::get();
		
		$result = WB_ObjFactory::factory( 'BidonClass' );		
        $this->assertEquals( 'BidonClass', get_class( $result ) );			
	}
		
	
	// can't use the following test because
	// exceptions can't be throwned in __construct & __destruct	
/*
	public function testConstructor()
	{
		$result = false;		
		try 
		{
			$obj = @new TestClass;
		}
		catch( Exception $e )
		{
			$result = true;
		}
        $this->assertEquals( true, $result );		
	}
*/

	// this test can not be used too.
/*
	public function testDefaultFactory()
	{
		$result = false;		
		try 
		{
			$obj = WB_ObjFactory::factory( 'bidon' );
		}
		catch( Exception $e )
		{
			$result = true;
		}
        $this->assertEquals( true, $result );		
	}
*/

// #####################################################################
// SINGLETON TESTS
// #####################################################################

	public function testSimpleSingleton()
	{
		$obj = WB_ObjFactory::factory( 'BidonClassSingleton' );		
		$this->assertEquals( true, $obj instanceof WB_Singleton_IF );		
	}
	

	public function testSingletonCreation1()
	{
		$result = true;		
		try 
		{
			$obj = WB_ObjFactory::factory( 'BidonClassSingleton' );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals( true, $result );		
		$this->assertEquals( true, $obj instanceof BidonClassSingleton );				
	}

	public function testSingletonCreation2()
	{
		$result = true;		
		try 
		{
			$obj = WB_ObjFactory::factory( 'BidonClassNotSingleton' );
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals( true, $result );		
		$this->assertEquals( true, $obj instanceof BidonClassNotSingleton );						
	}

	public function testSingletonCreation3()
	{
		$result = true;		
		try 
		{
			$obj1 = WB_ObjFactory::factory( 'BidonClassSingleton' );
			$obj2 = WB_ObjFactory::factory( 'BidonClassSingleton' );			
		}
		catch( Exception $e )
		{
			$result = false;
		}
        $this->assertEquals( true, $result );		
		$this->assertEquals( $obj1, $obj2 );				
		$this->assertEquals( true, $obj1 instanceof BidonClassSingleton );			
		$this->assertEquals( true, $obj2 instanceof BidonClassSingleton );					
	}

}